package cn.edu.xjtu.nbrash.xbarsuite.presms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.RashIntents;

public class RashPreSmsActivity extends Activity implements OnClickListener {

	private ListView listview;
	private Context context;
	private List<PreSMS> preSMSList;
	private List<PreSMS> selectedList;
	private boolean isMulChoice = false; // 是否多选
	private Adapter adapter;
	// private RelativeLayout layout;
	private Button cancel, delete, add, back, selectAll;
	private TextView txtcount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_presms_select);
		context = this;
		preSMSList = null;
		selectedList = new ArrayList<>();
		listview = (ListView) findViewById(R.id.list);
		// layout = (RelativeLayout) findViewById(R.id.relative);
		txtcount = (TextView) findViewById(R.id.txtcount);
		cancel = (Button) findViewById(R.id.cancle);
		delete = (Button) findViewById(R.id.delete);
		add = (Button) findViewById(R.id.presms_add);
		back = (Button) findViewById(R.id.presms_back_btn);
		selectAll = (Button) findViewById(R.id.presms_selectall_btn);
		cancel.setOnClickListener(this);
		delete.setOnClickListener(this);
		add.setOnClickListener(this);
		back.setOnClickListener(this);
		selectAll.setOnClickListener(this);
		show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(!isMulChoice){
			show();
		}
	}

	public void addNewPreSMS() {
		Intent intent = new Intent();
		intent.setClass(RashPreSmsActivity.this, EditPreSms.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("sms", new PreSMS("", ""));
		intent.putExtras(bundle);
		startActivity(intent);
	}

	private void show() {
		preSMSList = PreSMS.getPreSMSes(context,
				RashIntents.PreSMS.PRE_SMS_DATABASE);
		adapter = new Adapter(context, txtcount);
		listview.setAdapter(adapter);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancle:
			// isMulChoice = false;
			// selectedList.clear();
			// show();
			showStage();
			// layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.delete:
//			isMulChoice = true;
			PreSMS.deletePreSMSes(context, RashIntents.PreSMS.PRE_SMS_DATABASE,
					selectedList.toArray(new PreSMS[0]));
			selectedList.clear();
			txtcount.setText("共计");
			show();
			// layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.presms_add:
			addNewPreSMS();
			break;
		case R.id.presms_back_btn:
			if (isMulChoice) { // 在操作界面
				showStage();
			} else {
				finish();
			}
			break;
		case R.id.presms_selectall_btn:
			selectAll();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isMulChoice) { // 在操作界面
				showStage();
			} else {
				finish();
			}
		}
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("操作");
	}

	/**
	 * 自定义Adapter
	 */
	private void showStage() {
		isMulChoice = false;
		selectedList.clear();
		// layout.setVisibility(View.VISIBLE);
		for (int i = 0; i < preSMSList.size(); i++) {
			adapter.visiblecheck.put(i, CheckBox.GONE);
		}
		cancel.setVisibility(View.GONE);
		delete.setVisibility(View.GONE);
		txtcount.setVisibility(View.GONE);
		add.setVisibility(View.VISIBLE);
		back.setVisibility(View.VISIBLE);
		selectAll.setVisibility(View.GONE);
		show();
	}

	private void editStage() {
		isMulChoice = true;
		selectedList.clear();
		// layout.setVisibility(View.VISIBLE);
		for (int i = 0; i < preSMSList.size(); i++) {
			adapter.visiblecheck.put(i, CheckBox.VISIBLE);
		}
		cancel.setVisibility(View.VISIBLE);
		delete.setVisibility(View.VISIBLE);
		txtcount.setVisibility(View.VISIBLE);
		add.setVisibility(View.GONE);
		back.setVisibility(View.GONE);
		selectAll.setVisibility(View.GONE);
		show();
	}

	private void selectAll() {
		for(int i=0;i<adapter.getCount();i++){
			Integer temp = i;
			adapter.visiblecheck.remove(i);
			adapter.visiblecheck.put(temp, View.VISIBLE);
			adapter.ischeck.remove(temp);
			adapter.ischeck.put(temp, true);
		}
		Toast.makeText(context,
				 "进入了selectAll函数",
				Toast.LENGTH_LONG).show();
		adapter.notifyDataSetChanged();
	}

	private class Adapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater = null;
		private HashMap<Integer, View> mView;
		public HashMap<Integer, Integer> visiblecheck;// 用来记录是否显示checkBox
		public HashMap<Integer, Boolean> ischeck;
		private TextView txtcount;

		public Adapter(Context context, TextView txtcount) {
			this.context = context;
			this.txtcount = txtcount;
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mView = new HashMap<Integer, View>();
			visiblecheck = new HashMap<Integer, Integer>();
			ischeck = new HashMap<Integer, Boolean>();
			if (isMulChoice) {
				for (int i = 0; i < preSMSList.size(); i++) {
					ischeck.put(i, false);
					visiblecheck.put(i, CheckBox.VISIBLE);
				}
			} else {
				for (int i = 0; i < preSMSList.size(); i++) {
					ischeck.put(i, false);
					visiblecheck.put(i, CheckBox.INVISIBLE);
				}
			}
		}

		public int getCount() {
			return preSMSList.size();
		}

		public Object getItem(int position) {
			return preSMSList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			View view = mView.get(position);
			if (view == null) {
				view = inflater.inflate(R.layout.presms_item, null);
				TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
				TextView txtContent = (TextView) view
						.findViewById(R.id.txtContent);
				final CheckBox ceb = (CheckBox) view.findViewById(R.id.check);

				txtTitle.setText((preSMSList.get(position)).getTitle());
				txtContent.setText((preSMSList.get(position)).getContent());

				ceb.setChecked(ischeck.get(position));
				ceb.setVisibility(visiblecheck.get(position));

				// view.setOnLongClickListener(new Onlongclick());
				view.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View arg0) {
						// TODO Auto-generated method stub
						editStage();
						//selectedList.add(preSMSList.get(position));
						// 长按时选中这个东西还没有做好
						ceb.setChecked(true);
						adapter.notifyDataSetChanged();
						txtcount.setText("共计");
						return true;
					}

				});
				view.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						if (isMulChoice) {
							if (ceb.isChecked()) {
								ceb.setChecked(false);
								selectedList.remove(preSMSList.get(position));
							} else {
								ceb.setChecked(true);
								selectedList.add(preSMSList.get(position));
							}
							txtcount.setText("共选择了" + selectedList.size() + "项");
						} else {
							// Toast.makeText(context,
							// "点击了" + preSMSList.get(position),
							// Toast.LENGTH_LONG).show();
							Intent intent = new Intent();
							intent.setClass(RashPreSmsActivity.this,
									EditPreSms.class);
							Bundle bundle = new Bundle();
							bundle.putSerializable("sms",
									preSMSList.get(position));
							intent.putExtras(bundle);
							startActivity(intent);
						}
					}
				});

				mView.put(position, view);
			}
			return view;
		}

		// private class Onlongclick implements OnLongClickListener {
		//
		// public boolean onLongClick(View v) {
		// editStage();
		// selectedList.add(preSMSList.get(position));
		// return true;
		// }
		// }
	}

}
